
#include "OLInfoPanelSurface.h"
#include "OLApplication.h"
#include "OLInfoPanel.h"
#include "OLAppTemplateDefs.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QResizeEvent>

OLInfoPanelSurface::OLInfoPanelSurface(QWidget *parent) :
    OLModalBackground(parent), m_animGroup(0)
{
    m_infoPanel = new OLInfoPanel(this);

    connect(this, SIGNAL(clicked()), this, SLOT(hideInfoPanel()));
}

void OLInfoPanelSurface::showInfoPanel()
{
    // do a first handle of the orientation if required
    if(!m_infoPanel->layout()) {
        if(OLApp->orientation()==OLApplication::portrait)
            m_infoPanel->handlePortrait();
        else
            m_infoPanel->handleLandscape();
    }

    if(m_animGroup) {
        // already have an animation running, use the same simply switch direction
        QAbstractAnimation::Direction newDirection = m_animGroup->direction()==QAbstractAnimation::Forward ? QAbstractAnimation::Backward : QAbstractAnimation::Forward;
        m_animGroup->setDirection(newDirection);
    } else {
        // create a new animation
        show();

        m_animGroup = new QParallelAnimationGroup(this);
        //int animDuration = m_menupane->height()+100;
        int animDuration = 350;

        QPropertyAnimation* posAnim = new QPropertyAnimation(m_infoPanel, "pos", this);
        posAnim->setDuration(animDuration);
        posAnim->setEasingCurve(QEasingCurve::OutQuint);
        posAnim->setStartValue(QPoint(MENUPANE_WINDOWPADDING, -m_infoPanel->height()));
        posAnim->setEndValue(QPoint(MENUPANE_WINDOWPADDING, MENUPANE_OFFSET));

        m_animGroup->addAnimation(posAnim);

        QPropertyAnimation* opacityAnim = new QPropertyAnimation(m_infoPanel, "opacity", this);
        opacityAnim->setDuration(animDuration);
        opacityAnim->setEasingCurve(QEasingCurve::Linear);
        opacityAnim->setStartValue(0.0);
        opacityAnim->setEndValue(1.0);

        m_animGroup->addAnimation(opacityAnim);

        m_animGroup->start(QAbstractAnimation::DeleteWhenStopped);

        connect(m_animGroup, SIGNAL(finished()), SLOT(handleAnimFinished()));
    }
}

void OLInfoPanelSurface::hideInfoPanel()
{
    if(m_animGroup) {
        // already have an animation running, use the same simply switch direction
        QAbstractAnimation::Direction newDirection = m_animGroup->direction()==QAbstractAnimation::Forward ? QAbstractAnimation::Backward : QAbstractAnimation::Forward;
        m_animGroup->setDirection(newDirection);
    } else {
        m_animGroup = new QParallelAnimationGroup(this);
        //int animDuration = m_menupane->height()+100;
        int animDuration = 350;

        QPropertyAnimation* posAnim = new QPropertyAnimation(m_infoPanel, "pos", this);
        posAnim->setDuration(animDuration);
        posAnim->setEasingCurve(QEasingCurve::InQuint);
        posAnim->setStartValue(m_infoPanel->pos());
        posAnim->setEndValue(QPoint(MENUPANE_WINDOWPADDING, -m_infoPanel->height()));

        m_animGroup->addAnimation(posAnim);

        QPropertyAnimation* opacityAnim = new QPropertyAnimation(m_infoPanel, "opacity", this);
        opacityAnim->setDuration(animDuration);
        opacityAnim->setEasingCurve(QEasingCurve::Linear);
        opacityAnim->setStartValue(1.0);
        opacityAnim->setEndValue(0.0);

        m_animGroup->addAnimation(opacityAnim);

        m_animGroup->start(QAbstractAnimation::DeleteWhenStopped);

        connect(m_animGroup, SIGNAL(finished()), SLOT(handleAnimFinished()));
        connect(m_animGroup, SIGNAL(finished()), SLOT(deleteLater()));
    }
}

void OLInfoPanelSurface::resizeEvent(QResizeEvent * event)
{
    m_infoPanel->setFixedWidth(width()-(MENUPANE_WINDOWPADDING*2));
}

void OLInfoPanelSurface::handleAnimFinished()
{
    m_animGroup = 0;
}

void OLInfoPanelSurface::handleAppSize()
{
    resize(parentWidget()->size());
}
